package main

import "fmt"

//字典结构
type Dictionary struct {
	data map[interface{}]interface{} //键值都为interface{}类型
}

//根据键获取值
func (d *Dictionary) Get(key interface{}) interface{} {
	return d.data[key]
}

//设置键值
func (d *Dictionary) Set(key interface{}, value interface{}) {
	d.data[key] = value
}

//遍历所有的键值，如果回调返回为false,停止遍历
func (d *Dictionary) Visit(callback func(k, v interface{}) bool) {
	if callback == nil {
		return
	}

	for k, v := range d.data {
		if !callback(k, v) {
			return
		}
	}
}

//清空所有的数据
func (d *Dictionary) Clear() {
	d.data = make(map[interface{}]interface{})
}

//创建一个字典
func NewDictionary() *Dictionary {
	d := &Dictionary{}
	d.Clear()
	return d
}

func main() {
	//创建字典实例
	dict := NewDictionary()
	dict.Set("My Factory", 60)
	dict.Set("Terra Craft", 36)
	dict.Set("Dont't Hungry", 24)
	//获取值及打印
	favorite := dict.Get("Terra Craft")
	fmt.Println("favorite:", favorite)
	//遍历所有元素
	dict.Visit(func(k, v interface{}) bool {
		if v.(int) > 40 {
			fmt.Println(k, "is expensive")
			return true
		}
		//默认输出很便宜
		fmt.Println(k, "is cheap")
		return true
	})
}
